package com.leedcode;

import java.util.Arrays;
import java.util.HashSet;

/*
* 349. 两个数组的交集
* */
public class leedCode349 {
    public static void main(String[] args) {
        int[] nums1 = {1,2,2,1};
        int[] nums2 = {2,2};
        int[] arr = intersection(nums1, nums2);
        System.out.println(Arrays.toString(arr));
    }
    public static int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> hashSet1 = new HashSet<>();
        HashSet<Integer> result = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            hashSet1.add(nums1[i]);
        }

        for (int i : nums2) {
            if(hashSet1.contains(i)){
                result.add(i);
            }
        }
        int index=0;
        int[] resArr = new int[result.size()];
        for (Integer i : result) {
            resArr[index] = i;
            index++;
        }
        return resArr;
    }
}
